package miny_pascal;

import java_cup.runtime.Symbol;
import java.io.FileInputStream;
import java.io.InputStream;



%%
%cup
%line

%{

	public static void main(String args[]) throws Exception {
		InputStream is = new FileInputStream(args[0]);
		Yylex lexer = new Yylex(is);

		Symbol token = null;
		do {
			token = lexer.next_token();
			System.out.println(token == null ? "EOF" : token.toString());
		} while (token != null);
	}
	
	private int countLines(String str){
		int count = 0;
		for(int i = 0; i < str.length(); ++i){
			if(str.charAt(i) == '\n'){
				++count;
			}
		}
		return count;
	}
%}

DIGIT  =  [0-9]
INT    =  {DIGIT}+
LETTER =  [a-zA-Z_]
ID     =  {LETTER}({LETTER}|{DIGIT})*

%%

"IF"                    { return new Symbol(sym.IF); }
"DEF"                   { return new Symbol(sym.FUNCTION); }
"PASS"                  { return new Symbol(sym.PASS); }
"RET"                   { return new Symbol(sym.RET); }
{INT}					{ return new Symbol(sym.INTCONST, new Integer(Integer.parseInt(yytext()))); }
{ID}                    { return new Symbol(sym.IDENT, new String(yytext())); }
"+"						{ return new Symbol(sym.ADD); }
"-"						{ return new Symbol(sym.MIN); }
"*"						{ return new Symbol(sym.MUL); }
"("						{ return new Symbol(sym.LPAR); }
")"						{ return new Symbol(sym.RPAR); }
"{"						{ return new Symbol(sym.LC); }
"}"						{ return new Symbol(sym.RC); }
"["						{ return new Symbol(sym.LSQ); }
"]"						{ return new Symbol(sym.RSQ); }
"<"						{ return new Symbol(sym.LT); }
">"						{ return new Symbol(sym.GT); }
","                     { return new Symbol(sym.COMMA); }
";"                     { return new Symbol(sym.SEMICOLON); }
"="                     { return new Symbol(sym.ASSIGN); }
[\r\t\f\n\ ]+				{  }
.						{ System.err.println("unexpected char " + yytext() + " !\n"); System.exit(0); }

